import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  HttpStatus,
  HttpCode,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { Permissions } from '../../decorators/permissions.decorator';
import { TagService } from './tag.service';
import { 
  CreateTagDto, 
  UpdateTagDto, 
  TagResponseDto,
  AddTaskTagsDto,
  RemoveTaskTagDto
} from './dto';
import { plainToClass } from 'class-transformer';

@ApiTags('标签管理')
@Controller('tags')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth()
export class TagController {
  constructor(private readonly tagService: TagService) {}

  @Post()
  @Permissions('tag:create')
  @ApiOperation({ summary: '创建标签' })
  @ApiResponse({ 
    status: HttpStatus.CREATED, 
    description: '标签创建成功', 
    type: TagResponseDto 
  })
  async createTag(@Body() createTagDto: CreateTagDto) {
    const tag = await this.tagService.createTag(createTagDto);
    return plainToClass(TagResponseDto, tag);
  }

  @Get('workspace/:workspaceId')
  @Permissions('tag:read')
  @ApiOperation({ summary: '获取工作区内的所有标签' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: [TagResponseDto] 
  })
  async getWorkspaceTags(@Param('workspaceId') workspaceId: string) {
    const tags = await this.tagService.getWorkspaceTags(workspaceId);
    return tags.map(tag => plainToClass(TagResponseDto, tag));
  }

  @Get(':id')
  @Permissions('tag:read')
  @ApiOperation({ summary: '获取标签详情' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: TagResponseDto 
  })
  async getTagById(@Param('id') id: string) {
    const tag = await this.tagService.getTagById(id);
    return plainToClass(TagResponseDto, tag);
  }

  @Put(':id')
  @Permissions('tag:update')
  @ApiOperation({ summary: '更新标签' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '更新成功', 
    type: TagResponseDto 
  })
  async updateTag(@Param('id') id: string, @Body() updateTagDto: UpdateTagDto) {
    const tag = await this.tagService.updateTag(id, updateTagDto);
    return plainToClass(TagResponseDto, tag);
  }

  @Delete(':id')
  @Permissions('tag:delete')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '删除标签' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '删除成功' })
  async deleteTag(@Param('id') id: string) {
    await this.tagService.deleteTag(id);
  }

  @Post('task-tags')
  @Permissions('task:tag:create')
  @ApiOperation({ summary: '为任务添加标签' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '添加成功' })
  @HttpCode(HttpStatus.NO_CONTENT)
  async addTaskTags(@Body() addTaskTagsDto: AddTaskTagsDto) {
    await this.tagService.addTaskTags(addTaskTagsDto.taskId, addTaskTagsDto.tagIds);
  }

  @Delete('task-tag')
  @Permissions('task:tag:delete')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '移除任务标签' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '移除成功' })
  async removeTaskTag(@Body() removeTaskTagDto: RemoveTaskTagDto) {
    await this.tagService.removeTaskTag(
      removeTaskTagDto.taskId,
      removeTaskTagDto.tagId,
    );
  }

  @Get('task/:taskId')
  @Permissions('task:tag:read')
  @ApiOperation({ summary: '获取任务的所有标签' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: [TagResponseDto] 
  })
  async getTaskTags(@Param('taskId') taskId: string) {
    const tags = await this.tagService.getTaskTags(taskId);
    return tags.map(tag => plainToClass(TagResponseDto, tag));
  }
} 